package com.youfang.sixthmedia.ui.searcher;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.youfang.sixthmedia.CityActivity;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.HousefilterResponse;
import com.youfang.sixthmedia.message.HousefilterResponse.Area;
import com.youfang.sixthmedia.message.HousefilterResponse.HouseType;
import com.youfang.sixthmedia.message.HousefilterResponse.Price;

/**
 * 
 * @author teddy
 * 
 */
public class SearcherBuildingActivity extends BaseSearcherActivity {

	private int curCityId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.searcher_building_activity);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.city_container:
					gotoCityActivity();
					break;
				case R.id.area_container:
					gotoAreaActivity();
					break;
				case R.id.housetype_container:
					gotoHTActivity();
					break;

				case R.id.price_container:
					gotoPriceActivity();
					break;

				case R.id.searcher_btn:
					gotoSeacherResult();
					break;

				}

			}

		};

		for (int id : new int[] { R.id.city_container, R.id.area_container,
				R.id.housetype_container, R.id.price_container,
				R.id.searcher_btn }) {
			this.findViewById(id).setOnClickListener(listener);

		}

		curCityId = YoufangContext.getCity().cityid;
		this.remoteLoadHousefilter();
	}

	private static final int CITY_REQ_CODE = 1;
	private static final int AREA_REQ_CODE = 2;
	private static final int HT_REQ_CODE = 3;
	private static final int PRICE_REQ_CODE = 4;
	private static final int SEARCHER_RESULT_CODE = 5;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {

			switch (requestCode) {
			case CITY_REQ_CODE:
				// city changed
				if (this.curCityId != YoufangContext.getCity().cityid) {
					YoufangContext.resetSearcherFilter(((EditText) this
							.findViewById(R.id.keyword)).getText().toString());
					remoteLoadHousefilter();
				}
				break;
			case AREA_REQ_CODE:
				YoufangContext.searcher_curArea = (Area) data
						.getSerializableExtra("data");
				break;
			case HT_REQ_CODE:

				YoufangContext.searcher_curHT = (HouseType) data
						.getSerializableExtra("data");
				break;
			case PRICE_REQ_CODE:
				YoufangContext.searcher_curPrice = (Price) data
						.getSerializableExtra("data");

				break;
			case SEARCHER_RESULT_CODE:

				break;

			}
		}

		updateUI();

	}

	private void updateUI() {

		((TextView) this.findViewById(R.id.area))
				.setText(YoufangContext.searcher_curArea.cityname);
		((TextView) this.findViewById(R.id.ht))
				.setText(YoufangContext.searcher_curHT.housetypename);
		((TextView) this.findViewById(R.id.price))
				.setText(YoufangContext.searcher_curPrice.pricedesc);
		((TextView) this.findViewById(R.id.city)).setText(YoufangContext
				.getCity().name);

	}

	private void gotoSeacherResult() {

		YoufangContext.searcher_keyword = ((EditText) this
				.findViewById(R.id.keyword)).getText().toString();

		Intent intent = new Intent();
		intent.setClass(this, SearcherResultActivity.class);
		this.startActivityForResult(intent, SEARCHER_RESULT_CODE);
	}

	private void gotoCityActivity() {
		Intent intent = new Intent();
		intent.setClass(this, CityActivity.class);
		this.startActivityForResult(intent, CITY_REQ_CODE);

	}

	private void gotoAreaActivity() {
		Intent intent = new Intent();

		intent.setClass(this, SearcherAreaActivity.class);
		this.startActivityForResult(intent, AREA_REQ_CODE);

	}

	private void gotoHTActivity() {
		Intent intent = new Intent();
		intent.setClass(this, SearcherHTActivity.class);
		this.startActivityForResult(intent, HT_REQ_CODE);

	}

	private void gotoPriceActivity() {
		Intent intent = new Intent();
		intent.setClass(this, SearcherPriceActivity.class);
		this.startActivityForResult(intent, PRICE_REQ_CODE);

	}

	protected void onHousefilterLoaded(HousefilterResponse r) {
		curCityId = YoufangContext.getCity().cityid;
		updateUI();
	}

}
